﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Pages
{
    public partial class OrderInfo : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    if (!string.IsNullOrEmpty(Request["OrderID"]))
                    {
                        BindData(Request["OrderID"]);
                    }
                    else
                    {
                        Response.Redirect("/Pages/ShoppingList.aspx");
                    }
                }
                else
                {
                    Response.Redirect(URL_INDEX);
                }
            }
        }

        #region##绑定数据
        /// <summary>
        /// 绑定数据
        /// 创建人：Wilson
        /// 创建时间：2011-12-14
        /// </summary>
        /// <param name="orderId"></param>
        private void BindData(string orderId)
        {

            AUV5.Model.OrderInfo order = AUV5.BLL.OrderInfoBLL.Instance.GetDetail(orderId);

            if (order != null)
            {
                decimal balance = AUV5.BLL.OrderInfoBLL.Instance.GetBalanceByUserId(base.VerifiedUserID);   //余额

                lblBalance.Text = String.Format("{0:f2}", balance);

                decimal totalPayment = order.Total + order.ShipCharge;

                lblShipCharge.Text = String.Format("{0:f2}", order.ShipCharge);  //运费

                lblTotal.Text = String.Format("{0:f2}", totalPayment);  //总价
                
                AUV5.Model.OrderItem item = AUV5.BLL.OrderItemBLL.Instance.GetOrderItemByOrderID(orderId);

                if (item != null)
                {
                    AUV5.Model.ServiceInfo service = AUV5.BLL.ServiceInfoBLL.Instance.GetDetail(item.ServiceID);

                    if (service != null)
                    {
                        lblServiceTitle.Text = service.Title;
                        lblServiceDesc.Text = service.Description;
                        lblServicePrice.Text = String.Format("{0:f2}", service.Price);
                    }
                }

                AUV5.Model.UserInfo sellerInfo = AUV5.BLL.UserInfoBLL.Instance.GetDetail(order.Seller);

                if (sellerInfo != null)
                {
                    imgSeller.ImageUrl = sellerInfo.PhotoPath;   //头像
                }

                rptExtra.DataSource = AUV5.BLL.OrderItemBLL.Instance.GetOrderItemExtra(order.OrderID);
                rptExtra.DataBind();

                if (balance >= totalPayment)
                {
                    imgBtnBalance.Enabled = true;
                }
                else
                {
                    imgBtnBalance.Enabled = false;
                    imgBtnBalance.ImageUrl = "/!images/btn_notpay1.jpg";
                }
            }

        }
        #endregion


        protected void rptExtra_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label lblExtraId = e.Item.FindControl("lblExtraId") as Label;
                Label lblExtraTitle = e.Item.FindControl("lblExtraTitle") as Label;
                Label lblExtraPrice = e.Item.FindControl("lblExtraTitle") as Label;

                string extraId = lblExtraId.Text.Trim();

                AUV5.Model.ExtraService extra = AUV5.BLL.ExtraServiceBLL.Instance.GetDetail(extraId);

                if (extra != null)
                {
                    lblExtraTitle.Text = extra.Title;
                    lblExtraPrice.Text = Convert.ToString(extra.Price);
                }
            }
        }

        #region##余额支付
        /// <summary>
        /// 余额支付
        /// 创建人：Wilson
        /// 创建时间：2011-12-14
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnBalance_Click(object sender, ImageClickEventArgs e)
        {
            AUV5.Model.OrderInfo order = AUV5.BLL.OrderInfoBLL.Instance.GetDetail(Request["OrderID"]);

            if (order != null)
            {
                AUV5.Model.OrderItem orderItem = AUV5.BLL.OrderItemBLL.Instance.GetOrderItemByOrderID(order.OrderID);

                if (orderItem != null)
                {
                    AUV5.Model.ServiceInfo serviceInfo = AUV5.BLL.ServiceInfoBLL.Instance.GetDetail(orderItem.ServiceID);

                    if (serviceInfo != null)
                    {                    
                        order.BeginDate = DateTime.Now;

                        order.StatusID = Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment);

                        order.EndDate = AUV5.BLL.OrderInfoBLL.Instance.GetEndTimeByOrderCode(serviceInfo.ServiceID, order.OrderCode);

                        order.IsLocalPayment = true;

                        decimal paymentTotal = order.Total + order.ShipCharge;

                        order.Payment = paymentTotal;

                        AUV5.BLL.OrderInfoBLL.Instance.UpdateOrderInfoByOrderCode(order);

                        AUV5.BLL.BulletinBLL.Instance.AddBulletin("订单提示", string.Format("买家{0}为订单付款成功", VerifiedInfo.User.UserName), order.Seller, false, Convert.ToInt32(AUV5.Common.Enum.bulletinStatus.BeSeller), order.OrderID);
                    }
                    else
                    {
                        throw new Exception("服务不存在");
                    }
                }
                else
                {
                    throw new Exception("订单项不存在");
                }

                Response.Redirect("/Pages/OrderResult.aspx?OrderID=" + order.OrderID);
            }
            else
            {
                Alert("订单不存在");
            }
        }
        #endregion

        #region##去支付定支付
        /// <summary>
        /// 去支付定支付
        /// 创建人：Wilson
        /// 创建时间：2011-12-14
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAlipay_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/Alipay/Default.aspx?OrderID=" + Request["OrderID"]);
        }
        #endregion


    }
}